﻿using System.Diagnostics;

namespace ProjectEuler
{
    internal class Problem21
    {
        int[] numbers = new int[10001];
        bool[] visited = new bool[10001];

        public void Solve()
        {
            initNumbers();

            int sum=0;
            for (var i = 0; i < numbers.Length; i++)
            {
                if (visited[i])
                    continue;

                visited[i] = true;

                var sum1 = numbers[i];
                if (sum1 >=0 && sum1 < numbers.Length && sum1 != i)
                {
                    if (numbers[sum1] == i)
                    {
                        visited[sum1] = true;
                        sum += i;
                        sum += sum1;
                    }
                }
            }

            Debug.WriteLine("problem 21: {0}", sum);
        }



        void initNumbers()
        {
            for (var i = 0; i < numbers.Length; i++)
            {
                numbers[i] = Common.SumOfProperDivisors(i);
            }
        }
    }
}